package com.gyms.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.classic.okhttp.beans.HVMyMessageInfoBean;
import com.classic.okhttp.services.IUserService;
import com.classic.okhttp.zmoumall.http.ObjectCallback;
import com.classic.okhttp.zmoumall.utils.ToastUtil;
import com.gyms.R;
import com.gyms.adapter.MyMessageAdapter;
import com.gyms.base.MyAutoLayoutActivity;
import com.gyms.constant.Constants;
import com.gyms.manager.TitleManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import refresh.PtrClassicFrameLayout;
import refresh.PtrDefaultHandler;
import refresh.PtrFrameLayout;
import refresh.PtrHandler;
import utils.StringUtils;
import weight.CommonEmptyView;
import weight.head_foot_recyclerView.EndlessRecyclerOnScrollListener;
import weight.head_foot_recyclerView.HeaderAndFooterRecyclerViewAdapter;
import weight.head_foot_recyclerView.LoadingFooter;
import weight.head_foot_recyclerView.RecyclerViewStateUtils;

/**
 * 我的消息 yjl
 */
public class MyMessageActivity extends MyAutoLayoutActivity implements CommonEmptyView.UpdateClick {
    @BindView(R.id.rv_message)
    RecyclerView mRvMessage;
    @BindView(R.id.pf_message)
    PtrClassicFrameLayout mPtrMessage;
    @BindView(R.id.search_empty)
    CommonEmptyView searchEmpty;
    private List<HVMyMessageInfoBean> mdata = new ArrayList<>();
    private MyMessageAdapter mAdapter;
    //刷新
    private boolean isCanLoadMore=true;

    @Override
    protected void setTitleTheme() {
        mTheme = TitleManager.TitleTheme.White;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_my_message;
    }

    @Override
    protected void initView() {
        createLoadingView("加载中", true);
        setTitle("我的消息");
        setBack();
        initAdapter();
        searchEmpty.setUpDateClick(this);
    }

    private void initAdapter() {
        LinearLayoutManager mLLMMessage=new LinearLayoutManager(this);
        mLLMMessage.setOrientation(LinearLayoutManager.VERTICAL);
        mRvMessage.setLayoutManager(mLLMMessage);
        mAdapter = new MyMessageAdapter(mContext, mdata);
        HeaderAndFooterRecyclerViewAdapter headerAndFooterRecyclerViewAdapter = new HeaderAndFooterRecyclerViewAdapter(mAdapter);
        mRvMessage.setAdapter(headerAndFooterRecyclerViewAdapter);
        mRvMessage.addOnScrollListener(mOnScrollListener);
    }

    @Override
    protected void setListener() {
        mPtrMessage.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                doRefreshMessage();
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }
        });
    }

    private void doRefreshMessage() {
        isCanLoadMore=true;
        //刷新的时候将pageIndex始终为1
        mPageIndex = 1;
        mIsRefreshOrLoadMore = 0;
        getMessageList();
    }

    private void doLoadMore(){
        mPageIndex++;
        mIsRefreshOrLoadMore = 1;
        getMessageList();
    }


    @Override
    protected void processLogic() {
        loadingView.show();
        getMessageList();
    }

    private void getMessageList() {
        IUserService.getMyMessageList(this, mPageIndex, PAGESIZE, false, new ObjectCallback<ArrayList<HVMyMessageInfoBean>>() {
            @Override
            public void onSuccess(ArrayList<HVMyMessageInfoBean> response, String message) {
                dialogDis(mRvMessage,searchEmpty);
                if (mIsRefreshOrLoadMore == 1) {
                    RecyclerViewStateUtils.setFooterViewState(mRvMessage, LoadingFooter.State.Normal);
                } else if (mIsRefreshOrLoadMore == 0) {
                    mPtrMessage.refreshComplete();
                }
                if (null != response && !StringUtils.isNull(response)) {
                    if (mIsRefreshOrLoadMore == 0) {//刷新清空数据源
                        mdata.clear();
                    }
                    mdata.addAll(response);
                    mAdapter.notifyDataSetChanged();
                } else {
                    if (mIsRefreshOrLoadMore == 0) {
                        showEmptyView(mRvMessage,searchEmpty, 1);
                    } else {
                        isCanLoadMore=false;
                        doLoadMoreErro("没有更多的数据了");
                    }
                }
            }

            @Override
            public void onErrorT(int code, ArrayList<HVMyMessageInfoBean> response, String errorMessage) {
                dialogDis();
                showEmptyView(mRvMessage,searchEmpty, code);
            }

            @Override
            public void onHttpErrorCall(int statusCode) {
                dialogDis();
                showEmptyView(mRvMessage,searchEmpty, Constants.NO_NET);
            }
        });
    }

    /**
     * 加载更多出现错误的处理
     */
    private void doLoadMoreErro(String erroStr) {
        if (mIsRefreshOrLoadMore == 1) {
            mPageIndex--;
            if (mRvMessage != null)
                RecyclerViewStateUtils.setFooterViewState(mRvMessage, LoadingFooter.State.TheEnd);
            ToastUtil.showToast(mContext, erroStr);
        } else if (mIsRefreshOrLoadMore == 0) {
            if (mPtrMessage != null)
                mPtrMessage.refreshComplete();
        }
    }

    @Override
    public void onUpdateClick() {
        loadingView.show();
        getMessageList();
    }

    @Override
    protected void onDestroy() {
        mRvMessage.removeOnScrollListener(mOnScrollListener);
        super.onDestroy();
    }

    /**
     * 上拉加载监听
     */

    private EndlessRecyclerOnScrollListener mOnScrollListener = new EndlessRecyclerOnScrollListener() {

        @Override
        public void onLoadNextPage(View view) {
            super.onLoadNextPage(view);
            if(StringUtils.isNull(mRvMessage)){
                return;
            }
            LoadingFooter.State state = RecyclerViewStateUtils.getFooterViewState(mRvMessage);
            if(state == LoadingFooter.State.Loading) {
                Log.d("@Cundong", "the state is Loading, just wait..");
                return;
            }

            if (isCanLoadMore) {
                // loading more
                RecyclerViewStateUtils.setFooterViewState(MyMessageActivity.this, mRvMessage, PAGESIZE, LoadingFooter.State.Loading, null);
                doLoadMore();
            } else {
                //the end
                RecyclerViewStateUtils.setFooterViewState(MyMessageActivity.this, mRvMessage, PAGESIZE, LoadingFooter.State.TheEnd, null);
            }
        }
    };
}
